
rm(list=ls())

load("data/tmp/PanelMatch_temp1.RData")

##############################################################
### Getting basic matched sets to produce histograms

## getting matched sets for L3 West
matches_W_entry_L3_maha <- greens_west_entry$`3 0 yes mahalanobis 10 TRUE att`

## getting matched sets for L1 West
matches_W_entry_L1_maha <- greens_west_entry$`1 0 yes mahalanobis 10 TRUE att`

## getting matched sets for L3 East
matches_E_entry_L3_maha <- greens_east_entry$`3 0 yes mahalanobis 5 TRUE att`

## getting matched sets for L1 East
matches_E_entry_L1_maha <- greens_east_entry$`1 0 yes mahalanobis 5 TRUE att`

## extract objects to make histograms with
L3_maha_west_entry_sizes <- summary(matches_W_entry_L3_maha$att)$overview$matched.set.size
L1_maha_west_entry_sizes <- summary(matches_W_entry_L1_maha$att)$overview$matched.set.size
L3_maha_east_entry_sizes <- summary(matches_E_entry_L3_maha$att)$overview$matched.set.size
L1_maha_east_entry_sizes <- summary(matches_E_entry_L1_maha$att)$overview$matched.set.size

## Making the histograms with the objects extracted above
pdf(file = file.path("output/figures/", "figure_b3_entry.pdf"), width = 16, height = 10)
den.matrix <- matrix(1:2, nrow = 1, ncol = 2)
den.matrix <- 
  layout(
    den.matrix
  )
par(mar = c(4, 4, 6, 2), oma = c(3, 3, 3, 1))

hist(L3_maha_west_entry_sizes[L3_maha_west_entry_sizes>0], 
     col = "#62BAFF", border = NA,
     freq = TRUE,
     breaks = seq(1, 350, by = 20),
     xlim = c(0, 350), ylim= c(0, 170), xlab = "Number of Matched Control Units",
     main = "Greens Entry St. Parl",
     ylab = "Frequency",
     cex.main = 1.5, cex.lab = 1.2, cex.axis = 1.1)
lines(x = c(0,0), 
      y = c(0, length(L3_maha_west_entry_sizes[L3_maha_west_entry_sizes==0])), 
      lwd = 4,
      col = "#62BAFF")

white.hist <- hist(L1_maha_west_entry_sizes[L1_maha_west_entry_sizes>0] , 
                   col = rgb(1,1,1,0),     
                   breaks = seq(1, 350, by = 20),
                   xlim = c(0, 350), ylim= c(0, 170), xlab = "",
                   plot = F, lty = 1)
lines(white.hist$breaks, c(0, white.hist$counts),
      type = 'S', lty = 1)
legend(x = 150, y = 165,  
       legend = c(paste0("Three Election Lags"), 
                  paste0("One Election Lag")),
       fill = c("#62BAFF", "white"),
       xjust = 0,
       pt.cex = 1.2,
       bty = "n", ncol = 1, cex = 1.2, bg = "white")

mtext("West Germany (1969 - 2017)", side = 3, line = 4, outer = FALSE, cex = 1.4)

hist(L3_maha_east_entry_sizes[L3_maha_east_entry_sizes > 0], 
     col = "#62BAFF", border = NA,
     freq = TRUE,
     breaks = seq(1, 100, by = 5),
     xlim = c(0, 100), ylim= c(0, 80), xlab = "Number of Matched Control Units",
     main = "Greens Entry St. Parl",
     ylab = "Frequency",
     cex.main = 1.5, cex.lab = 1.2, cex.axis = 1.1)
lines(x = c(0,0), 
      y = c(0, length(L3_maha_east_entry_sizes[L3_maha_east_entry_sizes == 0])), 
      lwd = 4,
      col = "#62BAFF")

white.hist <- hist(L1_maha_east_entry_sizes[L1_maha_east_entry_sizes>0], 
                   col = rgb(1,1,1,0),      
                   breaks = seq(1, 100, by = 5),
                   xlim = c(0, 100), ylim= c(0, 80), xlab = "",
                   plot = F, lty = 1)
lines(white.hist$breaks, c(0, white.hist$counts),
      type = 'S', lty = 1)

legend(x = 40, y = 75,  
       legend = c(paste0("Three Election Lags"), 
                  paste0("One Election Lag ")),
       fill = c("#62BAFF", "white"),
       xjust = 0,
       pt.cex = 1.2,
       bty = "n", ncol = 1, cex = 1.2, bg = "white")

mtext("East Germany (1990 - 2017)", side = 3, line = 4, outer = FALSE, cex = 1.4)

dev.off()
